package com.suo.adapter;

import java.io.Serializable;
import java.util.List;

import com.db.JokeDBManager;
import com.suo.Joke;
import com.suo.JokeContent;
import com.suo.R;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class JokeAdapter extends BaseAdapter {

	private Context context;
	private List<Joke> jokeList;
	private LayoutInflater mInflater;
	@SuppressWarnings("unused")
	private JokeDBManager dbMgr;
	private OnClickListener onClickListener;
	
	public JokeAdapter(Context context, List<Joke> jokeList, OnClickListener onClickListener) {
		mInflater = LayoutInflater.from(context);
		this.context = context;
		this.jokeList = jokeList;
		this.onClickListener = onClickListener;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return jokeList.size();
	}

	@Override
	public Object getItem(int position) {
		return jokeList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		JokeViewHolder viewHolder ;
		final Joke joke = jokeList.get(position);
		dbMgr = new JokeDBManager(context);
		if(convertView == null)
		{
			convertView = mInflater.inflate(R.layout.joke_list, null);
			viewHolder = new JokeViewHolder();
			viewHolder.tvName = (TextView) convertView.findViewById(R.id.tvName);
			viewHolder.llJokeItem = (RelativeLayout) convertView.findViewById(R.id.llJokeItem);
			viewHolder.ivFavourite = (ImageView) convertView.findViewById(R.id.ivFavourite);
			viewHolder.ivDelete = (ImageView) convertView.findViewById(R.id.ivDelete);
			
			//Log.e("suo", "id:" + joke.id + "|name:" + joke.name);
			convertView.setTag(viewHolder);
		}
		else
		{
			viewHolder = (JokeViewHolder)convertView.getTag();
		}
		
		viewHolder.tvName.setText(joke.name);
		if(joke.type == 0)
		{
			viewHolder.ivFavourite.setImageResource(R.drawable.favourite_un);
		}else if(joke.type == 1)
		{
			viewHolder.ivFavourite.setImageResource(R.drawable.favourite);
		}
		
		viewHolder.ivFavourite.setTag(position);
		viewHolder.ivFavourite.setOnClickListener(onClickListener);
		
		viewHolder.ivDelete.setTag(R.id.tag_first,position);
		viewHolder.ivDelete.setOnClickListener(onClickListener);
		
		viewHolder.llJokeItem.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(context, JokeContent.class);
				intent.putExtra("id", joke.id);
				intent.putExtra("joke", (Serializable)jokeList);
				context.startActivity(intent);
			}
		});
        
        return convertView;
	}
	
	class JokeViewHolder
	{
		TextView tvName;
		RelativeLayout llJokeItem;
		ImageView ivFavourite;
		ImageView ivDelete;
	}
}
